package com.network.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.network.pojo.User;
import com.network.pojo.UserInfo;
import com.network.service.UserInfoService;
import com.network.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
//@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService userService;

    @Autowired
    UserInfoService userInfoService;



    @ResponseBody
    @GetMapping("/user/id")
    public List<UserInfo> searchById(@RequestParam("id") int id) {
        UserInfo userInfo = userInfoService.findUserInfoById(id);

        List<UserInfo> userInfos = new ArrayList<>();
        if(userInfo != null) {
            userInfos.add(userInfo);
        }
        return userInfos;
    }


    @GetMapping("/user/up")
    public String getUpdatePassShow() {

        return "updateUsername";
    }


    @ResponseBody
    @PutMapping("/user/up")
    public String updatePassword(@RequestParam("password") String password,@RequestParam("id") int id) {


        userService.updatePassword(id,password);

        return "success";
    }


    @ResponseBody
    @PutMapping("/userr")
    public UserInfo update(UserInfo userInfo, @RequestParam(value = "image",required = false) MultipartFile image, HttpSession session) {
        if (image != null) {

            String origin = image.getOriginalFilename();
            String suffix = origin.substring(origin.indexOf("."));
            System.out.println(suffix);
            String newName = UUID.randomUUID().toString() + suffix;

            //这一个部分是修改用户头像的，需要进行修改，打成jar
//            File file = new File("src/main/resources/pictures");
//            File file1 = new File(file.getAbsolutePath()+"/"+newName);
//            System.out.println(file1.getAbsolutePath());

//            Resource resource = new ClassPathResource("pictures");

            try {

                File file = new File("D:/pictures");


                image.transferTo(new File(file.getAbsolutePath()+"/"+newName));
//                image.transferTo(file1);
//                File file2 = new File(resource.getFile().getAbsoluteFile()+"/"+newName);
//                FileInputStream fileInputStream = new FileInputStream(file1);
//
//                FileOutputStream fileOutputStream = new FileOutputStream(file2);
//                byte[] bytes = new byte[1024];
//                int len = -1;
//                while ((len = fileInputStream.read(bytes)) != -1) {
//                    fileOutputStream.write(bytes,0,len);
//                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            userInfo.setPicture(newName);
        }

        userInfoService.updateUserInfo(userInfo);
        UserInfo userInfoById = userInfoService.findUserInfoById(userInfo.getId());
        session.setAttribute("user",userInfoById);
        return userInfoById;
    }

    @ResponseBody
    @GetMapping("/user/username")
    public List<UserInfo> searchByUsername(@RequestParam("username") String username) {
        UserInfo userInfo = userInfoService.findUserInfoByUsername(username);

        List<UserInfo> userInfos = new ArrayList<>();
        if(userInfo != null) {
            userInfos.add(userInfo);
        }
        return userInfos;
    }

    @ResponseBody
    @GetMapping("/user/name")
    public PageInfo<UserInfo> searchById(@RequestParam("name") String name
            , @RequestParam(value = "page") int page
            , @RequestParam(value = "limit") int limit
            , @RequestParam(value = "date") String date) {

        PageHelper.startPage(page,limit);
        List<UserInfo> userInfos = userInfoService.getUserInfoByName(name,new Date(date));
        PageInfo<UserInfo> listPageInfo = new PageInfo<>(userInfos);

        return listPageInfo;
    }


    @ResponseBody
    @GetMapping("/user/key")
    public PageInfo<UserInfo> searchByKey(@RequestParam("name") String name
            , @RequestParam(value = "page") int page
            , @RequestParam(value = "limit") int limit
            , @RequestParam(value = "date") String date) {

        PageHelper.startPage(page,limit);
        List<UserInfo> userInfos = userInfoService.getUserInfoByKey(name,new Date(date));
//        userInfos.forEach((e)->{
//            System.out.println(e);
//        });
        PageInfo<UserInfo> listPageInfo = new PageInfo<>(userInfos);

        return listPageInfo;
    }

    /**
     * 用户界面
     */
    @RequestMapping("/userInfo")
    public String userInfo() {
        return "userInfo";
    }



    @ResponseBody
    @GetMapping("/user/countAll")
    public int countUser(@RequestParam(value = "date") String date) {
        return userInfoService.countUser(new Date(date));
    }

    @ResponseBody
    @GetMapping("/user/countByName")
    public int countUser(@RequestParam(value = "date") String date,@RequestParam("name") String name) {
        return userInfoService.countUserByName(new Date(date),name);
    }









}
